package com.terrier.ui.controls
{
    import com.terrier.ui.events.WxButtonEvent;
    import com.terrier.ui.skins.IWxCheckButtonSkin;
    import com.terrier.ui.skins.WxSkinManager;
    import com.terrier.ui.skins.WxTextStyle;

    import flash.display.DisplayObject;
    import flash.events.Event;

    [Event(name = "change", type = "flash.events.Event")]

    /**
     * 可选按钮
     * @author Jarvis.weng
     */
    public class WxCheckButton extends WxLabelButton
    {
        private var _selected:Boolean = false;

        private var _autoToggle:Boolean = true;

        /**
         * 构造函数
         */
        public function WxCheckButton()
        {
            super();
        }

        /**
         * @private
         */
        override protected function initialize():void
        {
            super.initialize();
            if (_autoToggle)
            {
                addEventListener(WxButtonEvent.BUTTON_CLICK, clickHandler);
            }
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getCheckButtonSkin(skinID));
        }

        /**
         * 获取或设置一个布尔值，指示按钮能否进行自动切换。
         */
        public function get autoToggle():Boolean
        {
            return _autoToggle;
        }

        /**
         * @private
         */
        public function set autoToggle(value:Boolean):void
        {
            if (_autoToggle == value)
            {
                return;
            }
            _autoToggle = value;

            _autoToggle ? addEventListener(WxButtonEvent.BUTTON_CLICK, clickHandler) :
                removeEventListener(WxButtonEvent.BUTTON_CLICK, clickHandler);
        }

        /**
         * 获取或设置一个布尔值，指示按钮是否被选中
         * @default false
         * @return
         */
        public function get selected():Boolean
        {
            return _selected;
        }

        /**
         * @private
         */
        public function set selected(value:Boolean):void
        {
            _selected = value;

            invalidateView();

            dispatchEvent(new Event(Event.CHANGE));
        }

        /**
         * @private
         */
        override protected function getBgByPhase(phase:String):DisplayObject
        {
            var bg:DisplayObject;

            if (_selected)
            {
                switch (phase)
                {
                    case WxButtonPhase.UP:
                        bg = skin.selectedUpBg;
                        break;

                    case WxButtonPhase.OVER:
                        bg = skin.selectedOverBg;
                        break;

                    case WxButtonPhase.DOWN:
                        bg = skin.selectedDownBg;
                        break;

                    case WxButtonPhase.DISABLED:
                        bg = skin.selectedDisabledBg;
                        break;
                }
            }
            else
            {
                bg = super.getBgByPhase(phase);
            }

            return bg;
        }

        /**
         * @private
         */
        override protected function getTxtStyleByPhase(phase:String):WxTextStyle
        {
            if (skin == null)
            {
                return null;
            }

            var txtStyle:WxTextStyle;

            if (_selected)
            {
                switch (phase)
                {
                    case WxButtonPhase.UP:
                        txtStyle = skin.selectedUpTxtStyle;
                        break;

                    case WxButtonPhase.OVER:
                        txtStyle = skin.selectedOverTxtStyle;
                        break;

                    case WxButtonPhase.DOWN:
                        txtStyle = skin.selectedDownTxtStyle;
                        break;

                    case WxButtonPhase.DISABLED:
                        txtStyle = skin.selectedDisabledTxtStyle;
                        break;
                }
            }
            else
            {
                txtStyle = super.getTxtStyleByPhase(phase);
            }

            return txtStyle;
        }

        private function clickHandler(e:WxButtonEvent):void
        {
            selected = !selected;
        }

        private function get skin():IWxCheckButtonSkin
        {
            return _skin as IWxCheckButtonSkin;
        }
    }
}
